package com.example.testrxjava.register;

import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.example.testrxjava.R;
import com.example.testrxjava.base.BaseActivity;
import com.example.testrxjava.login.LoginActivity;
import com.example.testrxjava.util.Action;
import com.example.testrxjava.util.CommUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;


public class RegisterActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.usernameEdit)
    EditText usernameEdit;
    @BindView(R.id.passwordEdit)
    EditText passwordEdit;
    @BindView(R.id.passwordRepeatEdit)
    EditText passwordRepeatEdit;
    @BindView(R.id.registerBtn)
    TextView registerBtn;
    @BindView(R.id.loginBtn)
    TextView loginBtn;


    String username=null;//用户名
    String password=null;//密码
    String repassword=null;//确认密码
    private Map<String,String> map;

    private RegisterSucessCallBack callBack;

    @Override
    public int getLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
    }

    @Override
    public void init(Bundle savedInstanceState) {
        map=new HashMap<String,String>();
//        registerBtn=(TextView) findViewById(R.id.registerBtn);

        initLinstener();
    }

    //    @Override
//    public void init(s) {
//
//        map=new HashMap<String,String>();
//        initLinstener();
//    }
    private void saveUserInfo(){
        username=usernameEdit.getText().toString().trim();
        password=passwordEdit.getText().toString().trim();
        repassword=passwordRepeatEdit.getText().toString().trim();
        map.put("username",username);
        map.put("password",password);
        map.put("repassword",repassword);
    }
    @Override
    public void initLinstener() {
        registerBtn.setOnClickListener(this);
        loginBtn.setOnClickListener(this);
    }

//    @Override
//    public void sucess(RegisterBean bean) {
//
//    }
//
//    @Override
//    public void fail(String error) {
//
//    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.registerBtn:
                saveUserInfo();
                if(TextUtils.isEmpty(username)&&TextUtils.isEmpty(password)&&TextUtils.isEmpty(repassword)){
                    CommUtil.Toast(this,"输入不能为空!");
                    return;
                }
                if(!password.equals(repassword)){
                    CommUtil.Toast(this,"密码输入不一致!");
                    return;
                }

                Action.getInstance().register(map, new RegisterSucessCallBack() {
                    @Override
                    public void sucess(RegisterBean bean) {
                        Log.e("===",bean.getData().getUsername());
                        Log.e("===",bean.getData().getEmail());
                        Log.e("===",bean.getData().getPassword());
                        Log.e("===",bean.getData().getId()+"");
                        Log.e("===",bean.getData().getType()+"");

                        Intent intent=new Intent(RegisterActivity.this, LoginActivity.class);
                        startActivity(intent);
                        finish();


                    }

                    @Override
                    public void fail(String error) {
                        Log.e("====",error);
                        CommUtil.Toast(RegisterActivity.this,error);
                    }
                });

                break;
            case R.id.loginBtn:
                Intent intent=new Intent(RegisterActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();

                break;
        }
    }

}
